package com.topboya.linker.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.topboya.linker.R;
import com.topboya.linker.diyview.group.GroupRecyclerAdapter;
import com.topboya.linker.model.levelitem.SelectDeviceGroup;
import com.topboya.linker.model.response.NewlyOrgListResponse;
import com.topboya.linker.model.response.OrganizationResopnse;
import com.topboya.linker.ui.activity.xSelectVistorActivity;
import com.topboya.linker.utils.LogUtils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import retrofit2.http.POST;


public class SelectDevicesAdapter extends GroupRecyclerAdapter<String,String> {

    private static final String TAG = "SelectDevicesAdapter:";
    private Context mContext;
    private ArrayList<OrganizationResopnse.DocDetailInfoList> groupList;
    private ArrayList<NewlyOrgListResponse.Data> newlyList;
    private List<SelectDeviceGroup> selectDeviceGroups ;

    public SelectDevicesAdapter(Context context, ArrayList<OrganizationResopnse.DocDetailInfoList> groupList, ArrayList<NewlyOrgListResponse.Data> newlyList) {
        super(context);
        mContext = context;
        this.groupList = groupList;
        this.newlyList = newlyList;
        selectDeviceGroups = new ArrayList<>();
    }


    @Override
    protected RecyclerView.ViewHolder onCreateDefaultViewHolder(ViewGroup parent, int type) {
        LogUtils.v(TAG + "onCreateDefaultViewHolder");

        return new SelectDevicesViewHolder(mInflater.inflate(R.layout.create_device_item, parent, false));
    }

    @Override
    protected void onBindViewHolder(RecyclerView.ViewHolder holder, String item, int position) {
        LogUtils.v(TAG + "onBindViewHolder");
        SelectDevicesViewHolder h = (SelectDevicesViewHolder) holder;
//        h.tv_hasptial_level.setText(item);
        h.tv_hasptial_name.setText(item);
        holder.itemView.setOnClickListener(v -> {

        LogUtils.v(TAG + "position---->"+position+ "---groupList.get(position)："+selectDeviceGroups.get(position).getOrgName());
        Intent intent = new Intent(mContext,xSelectVistorActivity.class);
        intent.putExtra("DocDetailInfo",selectDeviceGroups.get(position));
        intent.putExtra("isCreateTask",true);

//        标记是是否是选择终端页面跳转到创建任务页面还是选择产品跳转到创建任务页面
        intent.putExtra("isDoc",true);
        mContext.startActivity(intent);

        });
    }


    private List<String> create(int p) {

        List<String> list = new ArrayList<>();
        if (p == 0) {
            for (int i=0;i < newlyList.size();i++){
//                LogUtils.v(TAG +"groupList.get(i).getOrgName():"+groupList.get(i).getOrgName());
                list.add(newlyList.get(i).getOrgName());
            }

        } else if (p == 1) {

                for (int i = 0; i < groupList.size(); i++) {
//                LogUtils.v(TAG +"groupList.get(i).getOrgName():"+groupList.get(i).getOrgName());
                    list.add(groupList.get(i).getOrgName() );

            }
        }
        return list;
    }

    public class SelectDevicesViewHolder extends RecyclerView.ViewHolder{

        //ViewHolder
        TextView tv_hasptial_level;
        TextView tv_hasptial_name;

        SelectDevicesViewHolder(View view) {

            super(view);
            this.tv_hasptial_level = view.findViewById(R.id.tv_hasptial_level);
            this.tv_hasptial_name = view.findViewById(R.id.tv_hasptial_name);

        }
    }


    public void resetGroups() {

        LinkedHashMap<String, List<String>> map = new LinkedHashMap<>();
        List<String> titles = new ArrayList<>();
        map.put("★ 最近", create(0));
        map.put("全部", create(1));
        titles.add("★ 最近");
        titles.add("全部");
//        将不同类型组成放到同一个数据里面，条目点击事件需要，可能框架有好的方法
        resetGroups(map,titles);
    }

    public void setItemData(List<SelectDeviceGroup> selectDeviceGroups ){

        this.selectDeviceGroups = selectDeviceGroups;
    }
}
